/*
 * InputArray.cpp
 *
 *  Created on: Mar 28, 2011
 *      Author: skyang
 */
#include "InputArray.h"

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;
void InputArray :: PrintElements(bool printWholeArray){

/*	int index;
	int ArrayLength;
	double CurrentArray [500];
	double PushElement;
	double PopElement;
	double ArrayMax;
	double ArrayMin;*/

	cout << "Current Index of Circular array is: " << index << endl;
	cout << "Array Length is: " << ArrayLength << endl;
	cout << "Minimum Element is: " << ArrayMin << endl;
	cout << "Maximum Element is: " << ArrayMax<< endl;
	cout << "Push Element is " << PushElement << endl;
	cout << "Pop Element is" << PopElement << endl;
	if (printWholeArray){
		cout << "Printing Entire Array" << endl;
		for (int i = 0; i<ArrayLength; i++){
			cout << "Array Element " << i << " is : " << CurrentArray[i] << endl;
		}
	}
	return;
}
bool InputArray :: InitiateArray(fstream & myfile){
	index = 0;
	double d;
	double max = 0;
	double min = 1.79769e+308;

	if(!myfile) //Always test the file open.
	    {
	          cout<<"Error opening output file"<<endl;
	          //system("pause");
	          return false;
	    }
	while(index<ArrayLength){

		if (myfile.eof()){
			cout << "Unexpected end of file"<< endl;
			return false;
		}

		myfile >> d;

		CurrentArray[index++] = d;
		if (d > max) max = d;
		if (d < min) min = d;
	}

	ArrayMax = max;
	ArrayMin = min;

	return true;
}



bool InputArray :: UpdateArray(fstream & myfile){

	//string str;
	double d;
	if (!myfile){
        cout<<"Error opening output file"<<endl;
        //system("pause");
        return false;
	}

	index = index % ArrayLength;

	if (myfile.eof()){
		cout << "Reached end of data." << endl;
		return false;
	}

	myfile >> d;

	// update the max and min elements.
	if (d > ArrayMax) ArrayMax = d;
	if (d < ArrayMax) ArrayMin = d;
	PopElement = CurrentArray[index];
	//getline(myfile, str)
	CurrentArray[index++] = d;
	//stringstream i(str);
	PushElement = d;
	return true;
}

double * InputArray :: getArray(){
	double * p;
	p = CurrentArray;
	return p;
}

double InputArray :: getPopElement(){
	return PopElement;
}

double InputArray :: getPushElement(){
	return PushElement;
}

int InputArray :: getArrayLength(){
	return ArrayLength;
}





